<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">

<head>
    <meta charset="UTF-8">
    <title>物品列表</title>

    <style>
        /* 全局样式 */
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f5f7fa;
            margin: 0;
            padding: 0;
            color: #333;
            line-height: 1.6;
        }

        /* 导航栏样式 */
        .navbar {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 12px 0;
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .navbar-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
        }

        .navbar-brand {
            font-size: 18px;
            font-weight: 600;
            color: #2c3e50;
            text-decoration: none;
            display: flex;
            align-items: center;
        }

        .navbar-brand i {
            margin-right: 8px;
            color: #2980b9;
        }

        .navbar-nav {
            display: flex;
            align-items: center;
        }

        .nav-link {
            color: #555;
            font-size: 14px;
            text-decoration: none;
            font-weight: 500;
            margin-left: 15px;
            transition: color 0.3s ease;
            padding: 5px 10px;
            border-radius: 4px;
        }

        .nav-link:hover {
            color: #2980b9;
            background-color: #f0f8ff;
        }

        .btn-group {
            display: flex;
            align-items: center;
            margin-left: 15px;
        }

        .btn-group .btn {
            margin-left: 8px;
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            white-space: nowrap;
        }

        .btn-primary {
            background-color: #3498db;
            color: white;
            border: none;
        }

        .btn-primary:hover {
            background-color: #2980b9;
        }

        .btn-outline-primary {
            background-color: transparent;
            color: #3498db;
            border: 1px solid #3498db;
        }

        .btn-outline-primary:hover {
            background-color: #3498db;
            color: white;
        }

        .btn-success {
            background-color: #2ecc71;
            color: white;
            border: none;
        }

        .btn-success:hover {
            background-color: #27ae60;
        }

        .btn-warning {
            background-color: #f39c12;
            color: white;
            border: none;
        }

        .btn-warning:hover {
            background-color: #d35400;
        }

        .btn-info {
            background-color: #3498db;
            color: white;
            border: none;
        }

        .btn-info:hover {
            background-color: #2980b9;
        }

        .btn-danger {
            background-color: #e74c3c;
            color: white;
            border: none;
        }

        .btn-danger:hover {
            background-color: #c0392b;
        }

        .welcome-text {
            margin-left: 15px;
            font-size: 14px;
            color: #555;
        }

        /* 主容器样式 */
        .container {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 20px;
            display: flex;
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 250px;
            background-color: #f9f9f9;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 20px;
            margin-right: 20px;
        }

        .sidebar-item {
            display: block;
            padding: 10px 15px;
            margin-bottom: 8px;
            background-color: white;
            border-radius: 5px;
            text-decoration: none;
            color: #555;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .sidebar-item:hover {
            background-color: #e8f5e9;
            color: #3a7d44;
        }

        /* 内容区域 */
        .content {
            flex: 1;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 25px;
        }

        h2 {
            color: #2c3e50;
            margin-bottom: 25px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        /* 表单样式 */
        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #555;
        }

        input[type="text"],
        input[type="number"],
        select {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }

        .btn {
            padding: 10px 15px;
            border-radius: 4px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .button-group {
            margin-top: 30px;
            display: flex;
            gap: 15px;
            justify-content: flex-end;
        }

        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f2f2f2;
            font-weight: 600;
        }

        tr:hover {
            background-color: #f9f9f9;
        }

        /* 按钮样式 */
        .btn {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
        }

        .btn-success {
            background-color: #2ecc71;
            color: white;
            border: none;
        }

        .btn-success:hover {
            background-color: #27ae60;
        }

        .btn-primary {
            background-color: #3498db;
            color: white;
            border: none;
        }

        .btn-primary:hover {
            background-color: #2980b9;
        }

        .btn-danger {
            background-color: #e74c3c;
            color: white;
            border: none;
        }

        .btn-danger:hover {
            background-color: #c0392b;
        }

        /* 状态按钮样式 */
        .status-btn {
            margin: 5px;
            padding: 5px 10px;
            cursor: pointer;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .btn-status-success {
            background-color: #2ecc71;
            color: white;
            border: none;
        }

        .btn-status-success:hover {
            background-color: #27ae60;
        }

        .btn-status-warning {
            background-color: #f39c12;
            color: white;
            border: none;
        }

        .btn-status-warning:hover {
            background-color: #d35400;
        }

        /* 模态框样式 */
        #confirmationModal {
            display: none;
            position: fixed;
            top: 20%;
            left: 50%;
            transform: translateX(-50%);
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
        }

        #confirmationModal p {
            margin-bottom: 15px;
        }

        #confirmBtn, #cancelBtn {
            padding: 6px 12px;
            border-radius: 4px;
            font-size: 13px;
            font-weight: 500;
            cursor: pointer;
        }

        #confirmBtn {
            background-color: #3498db;
            color: white;
            border: none;
            margin-right: 10px;
        }

        #confirmBtn:hover {
            background-color: #2980b9;
        }

        #cancelBtn {
            background-color: #ccc;
            color: white;
            border: none;
        }

        #cancelBtn:hover {
            background-color: #aaa;
        }

        /* 遮罩层样式 */
        .modal-backdrop {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 999;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }

            .sidebar {
                margin-right: 0;
                margin-bottom: 20px;
            }
        }
    </style>
</head>
<body>

<!-- 导航栏 -->
<nav class="navbar">
    <div class="navbar-container">
        <a class="navbar-brand" href="#">
            <i class="fas fa-flask"></i>
            实验室辅助管理系统
        </a>
        <div class="navbar-nav">
            <span sec:authorize="isAuthenticated()">
                <div class="btn-group">
                    <a href="/laboratory/Info" class="btn btn-sm btn-outline-success active">实验室管理</a>
                    <a href="/email" class="btn btn-sm btn-outline-info">邮箱</a>
                    <a href="/user/admin" class="btn btn-sm btn-outline-warning">管理员界面</a>
                    <a href="/user/userInfo" class="btn btn-sm btn-outline-primary">个人中心</a>
                    <span class="welcome-text">欢迎, <span sec:authentication="name"></span>!</span>
                    <a href="/user/logout" class="btn btn-sm btn-outline-danger">退出</a>
                </div>
            </span>
        </div>
    </div>
</nav>

<!-- 主容器 -->
<div class="container">
    <!-- 侧边栏 -->
    <div class="sidebar">
        <ul>
            <li><a href="/item/list" class="sidebar-item">物品管理</a></li>
            <li><a href="/safety-inspection/list" class="sidebar-item">安全检查登记记录</a></li>
            <li><a href="/safety-inspection/" class="sidebar-item">今日安全检查登记</a></li>
        </ul>
    </div>

    <!-- 内容区域 -->
    <div class="content">
        <h2>物品列表</h2>

        <!-- 搜索框 -->
        <form th:action="@{/item/list}" method="get">
            <div style="display: flex; flex-wrap: wrap; gap: 15px; margin-bottom: 20px;">
                <div class="form-group" style="flex: 1; min-width: 200px;">
                    <label for="status">状态</label>
                    <select id="status" name="status" style="width: 100%;">
                        <option value="">全部</option>
                        <option value="1" th:selected="${param.status == '1'}">正常</option>
                        <option value="0" th:selected="${param.status == '0'}">待维修</option>
                        <option value="2" th:selected="${param.status == '2'}">故障</option>
                    </select>
                </div>
                <div class="form-group" style="flex: 1; min-width: 200px;">
                    <label for="itemName">物品名称（模糊查询）</label>
                    <input type="text" id="itemName" name="itemName" th:value="${param.itemName}" placeholder="输入物品名称关键字" style="width: 100%;">
                </div>
                <div class="form-group" style="flex: 0 0 auto;">
                    <label>&nbsp;</label>
                    <div>
                        <button type="submit" class="btn btn-primary">搜索</button>
                        <a href="/item/list" class="btn btn-secondary" style="margin-left: 10px;">清空</a>
                    </div>
                </div>
            </div>
        </form>

        <!-- 物品列表表格 -->
        <table>
            <thead>
            <tr>
                <th>物品图片</th>
                <th>物品名称</th>
                <th>实验室名称</th>
                <th>数量</th>
                <th>购买日期</th>
                <th>状态</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="item : ${items}">
                <td>
                    <img th:src="@{${item.image}}" alt="物品图片" width="100" height="100">
                </td>
                <td th:text="${item.itemName}">物品名称</td>
                <td th:text="${labMap.get(item.laboratoryId)} ?: '未找到'">实验室名称</td>
                <td th:text="${item.quantity}">数量</td>
                <td th:text="${#temporals.format(item.purchaseDate, 'yyyy-MM-dd HH:mm')}">购买日期</td>
                <td th:text="${item.status == 1 ? '正常' : (item.status == 2 ? '损坏' : '未知')}">状态</td>
                <td th:text="${item.remark}">备注</td>
                <td>
                    <button class="btn btn-status-success approve-btn" th:data-id="${item.itemId}">通过</button>
                    <button class="btn btn-status-warning reject-btn" th:data-id="${item.itemId}">不通过</button>
                </td>
            </tr>
            </tbody>
        </table>

        <!-- 模态框 -->
        <div id="confirmationModal">
            <p id="confirmationText">确定要修改状态吗？</p>
            <button id="confirmBtn">确认</button>
            <button id="cancelBtn">取消</button>
        </div>

        <!-- 遮罩层 -->
        <div class="modal-backdrop" id="modalBackdrop"></div>
    </div>
</div>



<script>
    let currentItemId = null;
    let newStatus = null;

    // 点击【通过】按钮：状态改为 1（正常）
    $('.approve-btn').click(function() {
        currentItemId = $(this).data('id');
        newStatus = 1;
        $('#confirmationText').text('确定将该物品状态改为正常？');
        $('#confirmationModal').show();
        $('#modalBackdrop').show();
    });

    // 点击【不通过】按钮：状态改为 2（损坏）
    $('.reject-btn').click(function() {
        currentItemId = $(this).data('id');
        newStatus = 2;
        $('#confirmationText').text('确定将该物品状态改为损坏？');
        $('#confirmationModal').show();
        $('#modalBackdrop').show();
    });

    // 确认修改状态
    $('#confirmBtn').click(function() {
        if (currentItemId && newStatus !== null) {
            $.ajax({
                url: '/item/update/status/' + currentItemId + '/' + newStatus,
                type: 'POST',
                success: function(response) {
                    location.reload(); // 刷新页面获取最新数据
                },
                error: function(error) {
                    alert('状态更新失败');
                    console.error(error);
                }
            });
        }
        $('#confirmationModal').hide();
        $('#modalBackdrop').hide();
    });

    // 取消操作
    $('#cancelBtn').click(function() {
        $('#confirmationModal').hide();
        $('#modalBackdrop').hide();
    });

    // 点击遮罩层关闭模态框
    $('#modalBackdrop').click(function() {
        $('#confirmationModal').hide();
        $(this).hide();
    });
</script>

</body>
</html>